<?php

namespace App\Http\Middleware;

use Closure,Log;
use Exception;

class ApiAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (empty($request->users_id)) return response()->json(['status'=>'error','code'=>'50001','msg'=>'登录异常, 请重新登录!']);
        try {
            $users_id = decrypt($request->users_id);
            if (empty($users_id)) return response()->json(['status'=>'error','code'=>'50002','msg'=>'登录异常，请重新登录！']);
            if (!is_numeric($users_id)) return response()->json(['status'=>'error','code'=>'50002','msg'=>'登录异常，请重新登录！']);
            $request->merge(['users_id' => $users_id]);
        } catch (Exception $e) {
            return response()->json(['status'=>'error','code'=>'50003','msg'=>'登录异常，请重新登录！']);
        }
        return $next($request);
    }
}
